package com.gzx.qc.modular.keyword.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.trans.service.impl.TransService;
import com.google.common.collect.Lists;
import com.gzx.qc.modular.keyword.entity.GzxQaKeyword;
import com.gzx.qc.modular.keyword.entity.bo.GzxQaKeywordAddOrEditBo;
import com.gzx.qc.modular.keyword.entity.bo.GzxQaKeywordQueryBo;
import com.gzx.qc.modular.keyword.entity.vo.GzxQaKeywordImportVo;
import com.gzx.qc.modular.keyword.entity.vo.GzxQaKeywordVo;
import com.gzx.qc.modular.keyword.mapper.GzxQaKeywordMapper;
import com.gzx.qc.modular.keyword.service.GzxQaKeywordService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.util.CommonExcelUtil;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @date 2025/1/24 15:59
 */
@Service
@DS("qc")
@RequiredArgsConstructor
public class GzxQaKeywordServiceImpl implements GzxQaKeywordService {

    private final GzxQaKeywordMapper qaKeywordMapper;

    private final TransService transService;

    /**
     * 分页查询关键词
     *
     * @param keywordQueryBo
     * @return
     */
    @Override
    public Page<GzxQaKeywordVo> pageList(GzxQaKeywordQueryBo keywordQueryBo) {
        return qaKeywordMapper.selectVoPage(new Page<>(keywordQueryBo.getCurrent(), keywordQueryBo.getSize()), Wrappers.<GzxQaKeyword>lambdaQuery()
                .like(StringUtils.isNotEmpty(keywordQueryBo.getKeyword()), GzxQaKeyword::getKeyword, keywordQueryBo.getKeyword())
                .eq(StringUtils.isNotEmpty(keywordQueryBo.getLevel()), GzxQaKeyword::getLevel, keywordQueryBo.getLevel())
                .eq(StringUtils.isNotEmpty(keywordQueryBo.getType()), GzxQaKeyword::getType, keywordQueryBo.getType())
                .eq(StringUtils.isNotBlank(keywordQueryBo.getScope()), GzxQaKeyword::getScope, keywordQueryBo.getScope())
                .orderByDesc(GzxQaKeyword::getId));
    }

    /**
     * 获取所有关键字
     *
     * @return
     */
    @Override
    public Set<String> listAllKeyword(Integer scope) {
        return qaKeywordMapper.selectList(Wrappers.<GzxQaKeyword>lambdaQuery()
                        .eq(GzxQaKeyword::getScope, scope))
                .stream().map(GzxQaKeyword::getKeyword).collect(Collectors.toSet());
    }

    /**
     * 新增关键词
     *
     * @param keywordAddOrEditBo
     */
    @Override
    public void addOrEdit(GzxQaKeywordAddOrEditBo keywordAddOrEditBo) {
        GzxQaKeyword qaKeyword = MapstructUtils.convert(keywordAddOrEditBo, GzxQaKeyword.class);
        if (StringUtils.isNotEmpty(keywordAddOrEditBo.getId())) {
            qaKeywordMapper.updateById(qaKeyword);
        } else {
            qaKeywordMapper.insert(qaKeyword);
        }
    }

    /**
     * 关键词删除
     *
     * @param id
     */
    @Override
    public void del(Long id) {
        qaKeywordMapper.deleteById(id);
    }

    /**
     * 导入关键词
     *
     * @param file
     */
    @Override
    public void importExcel(MultipartFile file) {
        List<GzxQaKeywordImportVo> keywordImportVoList = CommonExcelUtil.importExcel(file, GzxQaKeywordImportVo.class);
        if (CollectionUtil.isNotEmpty(keywordImportVoList)) {
            transService.unTransMore(keywordImportVoList);
            Date currentTime = new Date();
            String loginId = StpLoginUserUtil.getLoginUser().getId();
            List<GzxQaKeyword> keywordList = new ArrayList<>();
            for (GzxQaKeywordImportVo keywordImportVo : keywordImportVoList) {
                if (StringUtils.isNotEmpty(keywordImportVo.getLevel()) && StringUtils.isNotEmpty(keywordImportVo.getType())) {
                    String[] split = keywordImportVo.getKeyword().split(",");
                    for (int i = 0; i < split.length; i++) {
                        GzxQaKeyword keyword = new GzxQaKeyword();
                        keyword.setKeyword(split[i]);
                        keyword.setLevel(Integer.valueOf(keywordImportVo.getLevel()));
                        keyword.setType(Integer.valueOf(keywordImportVo.getType()));
                        keyword.setScope(Integer.valueOf(keywordImportVo.getScope()));
                        keyword.setCreateTime(currentTime);
                        keyword.setCreateUser(loginId);
                        keywordList.add(keyword);
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(keywordList)) {
                Lists.partition(keywordList, 350).forEach(qaKeywordMapper::insertBatch);
            }
        }
    }
}
